package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingMapper;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 2024/7/8-13:50
 * 功能:
 */
@Service
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public void editNumberByOrderDate(OrderSetting orderSetting) {
        // 1.从数据库查询此日期是否已经有设置可预约数量
        long count =
                orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
        // 2.判断此日期是否有设置可预约数量
        if (count > 0) {
        // 3.如果此日期有设置可预约数量就更新
            orderSettingMapper.editNumberByOrderDate(orderSetting);
        } else {
        // 4.如果此日期没有设置可预约数量就添加
            orderSettingMapper.add(orderSetting);
        }
    }

    @Override
    public void add(List<OrderSetting> list) {

        for (OrderSetting orderSetting : list) {
            // 2.从数据库查询此日期是否已经有设置可预约数量
            long count =
                    orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
            // 3.判断此日期是否有设置可预约数量


            if (count > 0) {
                log.info("此日期有设置可预约数量就更新: {}", orderSetting.getNumber());
            // 4.如果此日期有设置可预约数量就更新
                orderSettingMapper.editNumberByOrderDate(orderSetting);
            } else {
                log.info("此日期没有设置可预约数量就添加: {}",orderSetting.getNumber());
            // 5.如果此日期没有设置可预约数量就添加
                orderSettingMapper.add(orderSetting);
            }
        }
    }


    @Override
    public List<OrderSetting> getOrderSettingByMonth(Date month) {
        // 1.通过Calendar对象得到本月第一天的日期
        Date begin = DateUtils.getFirstDay4Date(month);
        // 2.通过Calendar对象得到本月最后一天的日期
        Date end = DateUtils.getLastDay4Date(month);
        System.out.println("begin = " + begin);
        System.out.println("end = " + end);
        // 3.调用DAO根据日期查询预约设置数据
        List<OrderSetting> list = orderSettingMapper.getOrderSettingByMonth(begin,
                end);
        // 4.返回查询预约设置数据
        return list;
    }
}
